package com.ktjiaoyu.ktmall.service.impl;

import com.ktjiaoyu.ktmall.dao.ProductDao;
import com.ktjiaoyu.ktmall.dao.impl.ProductDaoImpl;
import com.ktjiaoyu.ktmall.entity.Product;
import com.ktjiaoyu.ktmall.service.ProductService;
import com.ktjiaoyu.ktmall.utils.Page;

import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {

    private final ProductDao productDao = new ProductDaoImpl();
    @Override
    public Page<Product> findList(String title, Long categoryId, int pageNo, int pageSize) throws SQLException {
        Page<Product> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int count = productDao.count(title, categoryId);
        page.setTotalCount(count);
        List<Product> list = productDao.findList(title, categoryId, page.getPageNo(), page.getPageSize());
        page.setData(list);
        return page;
    }

    @Override
    public int count(String title, Long categoryId) throws SQLException {
        return productDao.count(title, categoryId);
    }

    @Override
    public boolean saveOrUpdate(Product entity) throws SQLException {
        if (entity.getId() != null){
            return productDao.update(entity) > 0;
        }else {
            return productDao.insert(entity) > 0;
        }
    }
    @Override
    public Product findById(Long id) throws SQLException {
        return productDao.findById(id);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) throws SQLException {
        return productDao.updateStatus(id, status) > 0;
    }

    @Override
    public boolean removeById(Long id) throws SQLException {
        return productDao.delete(id) > 0;
    }
}
